package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.common.exception.BusinessException;
import com.company.project.common.exception.code.BaseResponseCode;
import com.company.project.entity.SysContentEntity;
import com.company.project.entity.SysStoreEntity;
import com.company.project.service.*;
import com.company.project.vo.req.goods.AlbumInsterReqVO;
import com.company.project.vo.req.goods.GoodsInsterReqVO;
import com.company.project.vo.req.goods.GoodsUpdateReqVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.SysGoodsMapper;
import com.company.project.entity.SysGoodsEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service("sysGoodsService")
@Slf4j
public class SysGoodsServiceImpl extends ServiceImpl<SysGoodsMapper, SysGoodsEntity> implements SysGoodsService {

    @Autowired
    private SysGoodsAlbumService sysGoodsAlbumService;
    @Autowired
    private SysStoreService sysStoreService;
    @Autowired
    private SysContentService sysContentService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private HttpSessionService httpSessionService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysGoodsEntity insterGoods(GoodsInsterReqVO vo, String storeId) {
        //  验证门店是否存在
        SysStoreEntity sysStoreEntity = checkStore(storeId);
        // 记录富文本
        SysContentEntity insertContentEntity = new SysContentEntity();
        insertContentEntity.setTitle("商品[" + sysStoreEntity.getId() + "][" + vo.getGoodsName() + "]");
        insertContentEntity.setContent(vo.getContent());
        insertContentEntity.setCreateTime(new Date());
        if (!sysContentService.save(insertContentEntity)) {
            new BusinessException(BaseResponseCode.DATA_BASE_ERROR.getCode(), "记录商品富文本异常");
        }

        SysGoodsEntity sysGoodsEntity = new SysGoodsEntity();
        // 插入商品
        BeanUtils.copyProperties(vo, sysGoodsEntity);
        sysGoodsEntity.setDeptId(deptService.rootDept());
        sysGoodsEntity.setContent(insertContentEntity.getId());
        sysGoodsEntity.setStoreId(sysStoreEntity.getId());
        sysGoodsEntity.setCheckCount(Long.valueOf(0));
        sysGoodsEntity.setCreateTime(new Date());

        boolean insterFlag = this.save(sysGoodsEntity);
        if (!insterFlag) {
            // 插入数据失败，// 抛出异常
            new BusinessException(BaseResponseCode.DATA_BASE_ERROR.getCode(), "录入商品信息失败");
        }
        // 插入商品相册
        List<AlbumInsterReqVO> albumInsterReqVOList = vo.getGoodsAlbum();
        sysGoodsAlbumService.insterAlbum(albumInsterReqVOList, sysGoodsEntity.getId());
        return sysGoodsEntity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGoods(GoodsUpdateReqVO vo, String storeId) {
        //  验证门店是否存在
        SysStoreEntity sysStoreEntity = checkStore(storeId);
        // 验证商品是否存在
        SysGoodsEntity sysGoodsEntity = this.checkGoodsError(vo.getId());
        // 富文本更新
        SysContentEntity sysContentEntity = new SysContentEntity();
        sysContentEntity.setId(sysGoodsEntity.getContent());
        sysContentEntity.setContent(vo.getContent());
        if (!sysContentService.updateById(sysContentEntity)) {
            new BusinessException(BaseResponseCode.DATA_BASE_ERROR.getCode(), "记录商品富文本异常");
        }
        // 更新商品信息。
        SysGoodsEntity uploadGoosdEntity = new SysGoodsEntity();
        uploadGoosdEntity.setStoreId(sysStoreEntity.getId());
        uploadGoosdEntity.setId(vo.getId());
        uploadGoosdEntity.setGoodsName(vo.getGoodsName());
        uploadGoosdEntity.setCoverImg(vo.getCoverImg());
        uploadGoosdEntity.setMoney(vo.getMoney());
        uploadGoosdEntity.setType(vo.getType());
        uploadGoosdEntity.setDescription(vo.getDescription());
        uploadGoosdEntity.setCreateTime(new Date());
        if (!super.updateById(uploadGoosdEntity)) {
            new BusinessException(BaseResponseCode.DATA_BASE_ERROR.getCode(), "更新商品信息失败");
        }
        // 删除商品相册，重新录入。
        sysGoodsAlbumService.deleteAlbum(vo.getId());
        //  重新录入商品相册
        List<AlbumInsterReqVO> albumInsterReqVOList = vo.getGoodsAlbum();
        sysGoodsAlbumService.insterAlbum(albumInsterReqVOList, sysGoodsEntity.getId());
    }

    @Override
    public SysGoodsEntity checkGoods(String goodsId) {
        return super.getById(goodsId);
    }

    private SysGoodsEntity checkGoodsError(String goodsId){
        SysGoodsEntity sysGoodsEntity = this.checkGoods(goodsId);
        if (null == sysGoodsEntity) {
            // 门店不存在 抛出异常。
            new BusinessException(BaseResponseCode.DATA_ERROR.getCode(), "商品不存在");
        }
        return sysGoodsEntity;
    }
    private SysStoreEntity checkStore(String storeId){
        SysStoreEntity sysStoreEntity = null;
        //  验证门店是否存在
        if (StringUtils.isEmpty(storeId)) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", httpSessionService.getCurrentUserId());
            sysStoreEntity = sysStoreService.getOne(queryWrapper);
        } else {
            sysStoreEntity = sysStoreService.getById(storeId);
        }
        if (null == sysStoreEntity) {
            // 门店不存在 抛出异常。
            new BusinessException(BaseResponseCode.DATA_ERROR.getCode(), "门店不存在");
        }
        return sysStoreEntity;
    }
}